package cn.com.nes.site.service.agent.impl;

import cn.com.nes.mybatis.agent.system.entity.TAgentFacturer;
import cn.com.nes.mybatis.agent.system.mapper.TAgentFacturerMapper;
import cn.com.nes.site.entity.dto.product.GGoodsDTO;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.service.agent.AgentFacturerService;
import cn.com.nes.site.service.agent.AgentGrantProductService;
import cn.com.nes.site.service.product.GGoodsService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AgentFacturerServiceImpl extends ServiceImpl<TAgentFacturerMapper, TAgentFacturer> implements AgentFacturerService {

    @Resource
    private AgentGrantProductService agentGrantProductService;

    @Resource
    private GGoodsService gGoodsService;

    @Override
    public Set<String> queryAgentCompanyInfoList(String companyId, String sblxId) throws Exception {
        // 查询全部代理商
        QueryWrapper<TAgentFacturer> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("agent_id");
        queryWrapper.eq("facturer_id", companyId);
        queryWrapper.eq("deleted", DeleteEnum._0.getKey());
        List<TAgentFacturer> tAgentFacturers = this.baseMapper.selectList(queryWrapper);

        Set<String> agentCompanyIds = tAgentFacturers.stream().map(TAgentFacturer::getAgentId).collect(Collectors.toSet());
        if (StrUtil.isBlank(sblxId)) {
            return agentCompanyIds;
        }

        // 获取产品uuid
        GGoodsDTO goodsDetail;
        try {
            goodsDetail = gGoodsService.getGoodsDetailBySblx(sblxId);
            if (ObjectUtil.isNull(goodsDetail)) {
                return agentCompanyIds;
            }
        } catch (Exception e) {
            return agentCompanyIds;
        }
        // 查询产品的代理关系
        Set<Long> agentRelationshipIds = agentGrantProductService.queryAgentRelationshipIds(goodsDetail.getUuid());
        if (CollectionUtil.isEmpty(agentRelationshipIds)) {
            return new HashSet<>();
        }
        // 查询授权代理商
        queryWrapper = new QueryWrapper<>();
        queryWrapper.select("agent_id");
        queryWrapper.eq("deleted", DeleteEnum._0.getKey());
        queryWrapper.in("id", agentRelationshipIds);
        queryWrapper.in("agent_id", agentCompanyIds);
        return this.baseMapper.selectList(queryWrapper).stream().map(TAgentFacturer::getAgentId).collect(Collectors.toSet());
    }

    @Override
    public List<String> queryAgentSupplierIds(Long callCenterCompanyId) {
        return this.baseMapper.queryAgentSupplierIds(callCenterCompanyId);
    }
}
